// pages/business/business.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: '0',
    mycurrent: '未处理',
    page: 1,
    list: [],
    myList: '',
    rowsTotal:'',
    clothingStyleList: app.globalData.clothingStyleList,
    user: wx.getStorageSync('user'),
    imgApi: app.globalData.imgApi

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getList(1)
    this.setData({
      user: wx.getStorageSync('user')
    })
    if (options.current!=undefined){
      this.setData({
        current: options.current
      })
    }
  },
  getList: function(page) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/list',
      data: {
        page: page,
        rows: 20
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            that.data.list[i].img = that.data.list[i].designImage.split(',')[0]
          }
          that.setData({
            list: that.data.list,
            rowsTotal: res.data.data.rowsTotal
          })
          that.data.page += 1
        }else{
          if (that.data.rowsTotal == '') {
            that.setData({
              list: []
            })
          }
        }
      }
    })
  },
  search:function(e){
    var that = this
    wx.request({
      url: app.globalData.api +'designDiagram/list',
      data: {
        page: 1,
        rows: 99,
        keyword: e.detail.value.key
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.data.list=[]
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            that.data.list[i].img = that.data.list[i].designImage.split(',')[0]
            
          }
          that.setData({
            list: that.data.list
          })
        }else{
          that.setData({
            list:[]
          })
        }
      }
    })
  },  
  disSear:function(e){
    if (e.detail.value == ''){
      this.data.list=[]
      this.data.page = 1
      this.getList(1)
    }
  },
  tabbarChange(e) {
    this.setData({
      current: e.detail.key,
    })
    if (e.detail.key == 2) {
      this.myList()
    }
  },
  MyonChange(e) {
    console.log('onChange', e)
    this.setData({
      mycurrent: e.detail.key,
    })
    this.myList()
  },
  add: () => {
    wx.navigateTo({
      url: '/pages/addTlitl/addTlitl?id=" "&index=" "'
    })
  },
  goDet: function(e) {
    wx.navigateTo({
      url: '/pages/details/details?id=' + e.currentTarget.dataset.id
    })
  },
  //提交
  subm: function() {
    var that = this
    if (app.globalData.clothingStyleList.length==0){
      wx.showToast({
        title: '请添加款式',
      })
      return
    }
    wx.request({
      url: app.globalData.api + 'businessOrderForm/add',
      data: {
        clothingStyleList: app.globalData.clothingStyleList
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.msg,
          })
          app.globalData.clothingStyleList = []
          that.setData({
            clothingStyleList: app.globalData.clothingStyleList
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.errorMsg,
          })
        }
      }
    })
  },
  //删除
  dele: function(e) {
    const {
      clothing,
      file
    } = e.target.dataset
    app.globalData.clothingStyleList = clothing.filter((n) => n.styleNumber !== file.styleNumber)
    this.setData({
      clothingStyleList: app.globalData.clothingStyleList
    })
  },
  //编辑
  edlit: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/addTlitl/addTlitl?id=' + e.target.dataset.id + '&index=' + e.target.dataset.index
    })
  },
  //获取列表
  myList: function() {
    var that = this
    wx: wx.request({
      url: app.globalData.api + 'businessOrderForm/myList',
      data: {
        "keyword": "",
        "orderBy": "",
        "status": this.data.mycurrent,
        "timeRange": "",
        "page": "",
        "rows": 99
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            myList: res.data.data.businessOrderFormList
          })
        }else{
          that.setData({
            myList:[]
          })
        }
      }
    })
  },
  goImg(e) {
    wx.request({
      url: app.globalData.api + 'designDiagram/findDesignDiagramStyleNumber/' + e.currentTarget.dataset.imgid,
      data: '',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.navigateTo({
            url: '/pages/details/details?id=' + res.data.data.id,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.errorMsg,
          })
        }

      }
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      clothingStyleList: app.globalData.clothingStyleList,
      user: wx.getStorageSync('user')
    })
    this.myList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.current == 0) {
      if (this.data.rowsTotal > this.data.list.length) {
        this.getList(this.data.page)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})